#include "StdAfx.h"
#include "H264FrameReader.h"
#include <algorithm>
#ifdef min
#undef min
#endif



H264FrameReader::H264FrameReader(void)
{
	m_pDataPointer = 0; 
	m_pEODPointer = 0; 
	m_bStop = false;
	m_streamPosition = 0;
}

H264FrameReader::~H264FrameReader(void)
{
}

void H264FrameReader::PutData(Ipp8u *data, Ipp32u dataLen)
{
	m_pDataPointer = data;
	m_pEODPointer  = data + dataLen;
}

Ipp32u H264FrameReader::GetRemainDataSize()
{
	if (m_pDataPointer == NULL || m_pEODPointer <= m_pDataPointer)
		return 0;

	return m_pEODPointer - m_pDataPointer;
}

Status H264FrameReader::ReadData(void *data, Ipp32u *nsize) 
{ 
	if (m_pDataPointer == NULL || m_pEODPointer <= m_pDataPointer)
	{
		*nsize = 0;

		return UMC_ERR_END_OF_STREAM;
	}

	Ipp32u bytesNumToCopy = std::min(*nsize, (Ipp32u) (m_pEODPointer - m_pDataPointer));

	memcpy(data, m_pDataPointer, bytesNumToCopy);
	//std::copy(m_pDataPointer, m_pDataPointer + bytesNumToCopy, (Ipp8u *) data);

	m_pDataPointer += bytesNumToCopy;
	m_streamPosition += bytesNumToCopy;
	*nsize = bytesNumToCopy;

	return UMC_OK;
}

Ipp64u H264FrameReader::GetPosition(void)
{ 
	return m_streamPosition;
}
